package com.example.demo.contorller;

import com.example.demo.bean.Regional;
import com.example.demo.bean.SecurityDomain;
import com.example.demo.common.ResponseCode;
import com.example.demo.common.ServerResponse;
import com.example.demo.service.RegionalService;
import com.example.demo.service.SecurityDomainService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/23
 */
@RequestMapping("/security_domain/")
@RestController
@AllArgsConstructor
public class SecurityDomainContorller {

    private final SecurityDomainService securityDomainService;
    @GetMapping(value = "security_domain_list")
    public ServerResponse<List<SecurityDomain>> securityDomainList(Integer parentId) {
        return securityDomainService.list(parentId);
    }

    @GetMapping(value = "select_find_one")

    public ServerResponse selectOne(Integer id){
        return securityDomainService.findOne(id);
    }

    @PostMapping("add_security_domain")
    public ServerResponse<SecurityDomain> addSecurityDomain(@Valid SecurityDomain securityDomain, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return securityDomainService.addSecurityDomain(securityDomain);
    }

    @DeleteMapping("delete_security_domain/{id}")
    public void deleteSecurityDomain(@PathVariable("id") Integer id) {
        securityDomainService.deleteSecurityDomain(id);
    }
    @PutMapping("security_domain_update")
    public ServerResponse<SecurityDomain> SecurityDomainUpdate(SecurityDomain securityDomain) {
        ServerResponse<SecurityDomain> responseSecurityDomain = securityDomainService.SecurityDomainUpdate(securityDomain);
        return responseSecurityDomain;
    }
}
